package com.boco.pciv.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.boco.pciv.controller.PcivController;
import com.boco.pciv.core.AppProperties;
import com.boco.pciv.core.entity.*;
import com.boco.pciv.entity.common.MenuInfo;
import com.boco.pciv.entity.sys.*;
import com.boco.pciv.service.sys.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 操 on 2017/01/17.
 */
@RestController
@RequestMapping(value="/sys/common")
public class DataComonController extends PcivController {
    Logger logger = LoggerFactory.getLogger(DataComonController.class);
    @Resource
    private IRoleService roleSvr;
    @Resource
    private IDataDictService ddSvr;
    @Resource
    private IDeptService deptSvr;
    @Resource
    private IResService resSvr;
    @Resource
    private ICityService citySvr;
    @Resource
    private IUserService userSvr;
    @Autowired
    private AppProperties appProperties;

    @GetMapping("/sysName")
    public String getSysName() {
        return appProperties.getName();
    }

    @GetMapping(value="/list4Select")
    public BizResponse getList4Select(String type, Integer hasAll){
        //下拉框
        List<SelectItem> list = new ArrayList<SelectItem>();
        if (hasAll !=null && hasAll ==1){
            list.add(new SelectItem("","全部", type));
        }
        if (type.equalsIgnoreCase("Role")) {
            //角色
            List<RoleInfo> roleInfos = roleSvr.getAll();
            for (RoleInfo role : roleInfos) {
                list.add(new SelectItem(role.getRoleId(), role.getRoleName(), type));
            }

        } else if(type.equalsIgnoreCase("DDType")) {
            //数据字典类型
            List<DictTypeInfo> ddInfos = ddSvr.getDDTypeList();
            for (DictTypeInfo dd : ddInfos) {
                list.add(new SelectItem(dd.getTypeCode(), dd.getTypeName(), type));
            }
        }
        else if(type.equalsIgnoreCase("DDType_Muti")) {
            //数据字典类型 -多层
            List<DictTypeInfo> ddInfos = ddSvr.getDDTypeList();
            for (DictTypeInfo dd : ddInfos) {
                if(dd.getMutiFlag()==1) {
                    list.add(new SelectItem(dd.getTypeCode(), dd.getTypeName(), type));
                }
            }
        }
        else if(type.equalsIgnoreCase("DDType_Single")) {
            //数据字典类型 -单层
            List<DictTypeInfo> ddInfos = ddSvr.getDDTypeList();
            for (DictTypeInfo dd : ddInfos) {
                if(dd.getMutiFlag()==0) {
                    list.add(new SelectItem(dd.getTypeCode(), dd.getTypeName(), type));
                }
            }
        }
        else if(type.equalsIgnoreCase("Dept")) {
            //部门
            List<DeptInfo> ddInfos = deptSvr.getAll();
            for (DeptInfo dd : ddInfos) {
                list.add(new SelectItem(dd.getId(), dd.getName(), type));
            }
        }
        else if(type.equalsIgnoreCase("City")) {
            //部门
            List<CityInfo> ncCounty = citySvr.getCity();
            for (CityInfo dd : ncCounty) {
                list.add(new SelectItem(dd.getId(), dd.getName(), type));
            }
        }

        return BizResponseUtil.list(list);
    }

    @GetMapping(value="/dd4Select")
    public BizResponse getDDList4Select(String ddtype, String parentId, Integer hasAll){
        //下拉框
        List<SelectItem> list = new ArrayList<SelectItem>();
        if (hasAll !=null && hasAll ==1){
            list.add(new SelectItem("","请选择", ddtype));
        }
        if (StrUtil.isBlank(parentId)) parentId="00";
        List<DataDict> result=ddSvr.getListByType(ddtype,parentId);
        for (DataDict dd : result) {
            list.add(new SelectItem(dd.getDictKey(), dd.getDictValue(), ddtype));
        }
        return BizResponseUtil.list(list);
    }
    @GetMapping("/resList")
    public BizResponse getResourceList(){
        SearchCriteria param = new SearchCriteria();
        param.setIsPage(false);
        List<ResInfo> list = resSvr.getList(param);
        return BizResponseUtil.list(list);
    }
    @GetMapping("/resTree")
    public BizResponse getResourceTree(){
        //BizTree tree = resSvr.getEasyUITreeByParent(Constants.ModulesParentID);
        ResTree tree = resSvr.getResTreeByParent(Constants.ModulesParentID);
        if (tree!=null){
            return BizResponseUtil.list(tree.getChildren());
        }
        else {
            return BizResponseUtil.list(new ArrayList());
        }
    }
    @GetMapping("/roleResTree")
    public BizResponse getRoleResTree(String roleId){
        ResTree tree = resSvr.getResTreeByRole(roleId);
        if (tree!=null){
            return BizResponseUtil.list(tree.getChildren());
        }
        else {
            return BizResponseUtil.list(new ArrayList());
        }
    }

    @GetMapping("/user4Select")
    public BizResponse getUser4Select(String deptid, Integer hasAll){
        //下拉框
        List<SelectItem> list = new ArrayList<SelectItem>();
        if (hasAll !=null && hasAll ==1){
            list.add(new SelectItem("","全部", "User"));
        }
        SearchCriteria param = new SearchCriteria();
        if (StrUtil.isBlank(deptid)) {
            param.putCriteria("deptId",deptid);
        }
        param.setIsPage(false);
        List<UserInfo> users = userSvr.getList(param);
        for (UserInfo dd : users) {
            list.add(new SelectItem(dd.getUserId(), dd.getUserName(), "User"));
        }
        return BizResponseUtil.list(list);
    }

    @GetMapping("/deptTree4Select")
    public BizResponse getDeptTree4Select(String parentId){
        List<SelectItem> list = deptSvr.getDeptTreeByParent4Select(parentId);
        return BizResponseUtil.list(list);
    }

    @GetMapping("/menu")
    public BizResponse getMenu(){
        List<MenuInfo> rtn = new ArrayList<>();
        List<ResInfo> list = resSvr.getMenuListByUser(getCurUserId());
        for (ResInfo info : list) {
            MenuInfo m = new MenuInfo();
            m.setId(info.getId());
            m.setTitle(info.getName());
            rtn.add(m);
        }
        return BizResponseUtil.list(rtn);
    }
    @GetMapping("/menuNav")
    public BizResponse getMenuNav(String pid){
        ResTree tree = resSvr.getResTreeByUserAndParent(getCurUserId(), pid);
        return BizResponseUtil.list(tree.getChildren());
    }
}
